package com.wondertek.web.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wondertek.common.base.Result;

import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.entity.sys.SysDictPo;
import com.wondertek.service.dto.sys.req.*;
import com.wondertek.service.service.sys.ISysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/dict")
@Tag(name = "系统字典管理")
public class SysDictController {

    @Resource
    private ISysDictService sysDictService;

    /**
     * 分页查询子级字典
     * @return /
     */
    @GetMapping("/findChildrenPage")
    @Operation(summary = "分页查询子级字典")
    @PreAuthorize("@el.check('backend:sys:dict:findChildrenPage')")
    public Result<Page<SysDictPo>> findChildrenPage(SysDictQueryChildrenReq sysDictQueryChildrenReq){
        return Result.success(sysDictService.findChildrenPage(sysDictQueryChildrenReq));
    }

    /**
     * 查询所有字典
     * @return /
     */
    @GetMapping("/findTree")
    @Operation(summary = "查询所有字典")
    public Result<List<SysDictPo>> tree(SysDictQueryReq sysDictQueryReq){
        return Result.success(sysDictService.findTree(sysDictQueryReq));
    }

    /**
     * 字典code查询字典
     * @return /
     */
    @GetMapping("/findListByDictCode")
    @Operation(summary = "字典code查询字典")
    public Result<List<SysDictPo>> findListByDictCode(@RequestParam("dictCode") String dictCode){
        return Result.success(sysDictService.findListByDictCode(dictCode));
    }

    /**
     * 新增字典
     * @param sysDictAddReq /
     * @return /
     */
    @PostMapping("/add")
    @Operation(summary = "新增字典")
    @AutoLog(category = "sys", handleType = "log.dict.insert",
            handleNote = "'新增字典:'+#sysDictAddReq.dictName")
    @PreAuthorize("@el.check('backend:sys:dict:add')")
    public Result<?> add(@Validated @RequestBody SysDictAddReq sysDictAddReq){
        sysDictService.add(sysDictAddReq);
        return Result.success();
    }

    /**
     * 修改字典
     * @param sysDictUpdateReq /
     * @return /
     */
    @PostMapping("/update")
    @Operation(summary = "修改字典")
    @AutoLog(category = "sys", handleType = "log.dict.update",
            handleNote = "'修改字典:'+#sysDictUpdateReq.dictName")
    @PreAuthorize("@el.check('backend:sys:dict:update')")
    public Result<?> update(@Validated @RequestBody SysDictUpdateReq sysDictUpdateReq){
        sysDictService.update(sysDictUpdateReq);
        return Result.success();
    }

    /**
     * 禁用字典
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/disable")
    @Operation(summary ="禁用字典")
    @AutoLog(category = "sys", handleType = "log.dict.disable",
            handleNote = "'禁用字典'")
    @PreAuthorize("@el.check('backend:sys:dict:disable')")
    public Result<?>  disable(@RequestBody @Validated BaseUpdateReq baseUpdateReq){
        sysDictService.disable(baseUpdateReq.getId());
        return  Result.success();
    }

    /**
     * 启用字典
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/enable")
    @Operation(summary ="启用字典")
    @AutoLog(category = "sys", handleType = "log.dict.enable",
            handleNote = "'启用字典'")
    @PreAuthorize("@el.check('backend:sys:dict:enable')")
    public Result<?>  enable(@RequestBody @Validated BaseUpdateReq baseUpdateReq){
        sysDictService.enable(baseUpdateReq.getId());
        return  Result.success();
    }

    /**
     * 删除字典
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/delete")
    @Operation(summary ="删除字典")
    @AutoLog(category = "sys", handleType = "log.dict.delete",
            handleNote = "'删除字典'")
    @PreAuthorize("@el.check('backend:sys:dict:delete')")
    public Result<?>  delete(@RequestBody @Validated BaseUpdateReq baseUpdateReq ){
        sysDictService.delete(baseUpdateReq.getId());
        return  Result.success();
    }

}
